package cn.yeyiu.netonlinevideo.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;

import cn.yeyiu.netonlinevideo.R;
import cn.yeyiu.netonlinevideo.activity.SingleTabDataListActivity;
import cn.yeyiu.netonlinevideo.activity.TypeListActivity;
import cn.yeyiu.netonlinevideo.bean.SourceData;
import cn.yeyiu.netonlinevideo.util.ParserFactory;

public class OnlineSourceAdapter extends RecyclerView.Adapter<OnlineSourceAdapter.ViewHolder> {

    private final Context mContext;

    public OnlineSourceAdapter(Context context) {
        mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View root = LayoutInflater.from(mContext).inflate(R.layout.item_data_src, parent, false);

        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SourceData source = ParserFactory.getSource(position);
        holder.title.setText(source.name);
        if ("local".equals(source.code)) {
            Glide.with(mContext)
                    .load(R.drawable.pic_logo)
                    .into(holder.img);
        } else {
            Glide.with(mContext)
                    .load(source.cover)
                    .into(holder.img);
        }

        holder.itemView.setOnClickListener(v -> {
            // 判断源类型 选择特定展示方法
            Intent i;
            if(source.serial){
                i = new Intent(mContext, TypeListActivity.class);
            }else{
                i = new Intent(mContext, SingleTabDataListActivity.class);
            }
            i.putExtra("src", JSONObject.toJSONString(source));
            mContext.startActivity(i);
        });
    }

    @Override
    public int getItemCount() {
        return ParserFactory.getSourcesCount();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView img;
        public TextView title;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            title = itemView.findViewById(R.id.title);
        }
    }
}
